package com.weimi.phh.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatEditor {

    /**
     * 一分钟的毫秒值，用于判断上次的更新时间
     */
    public static final long ONE_MINUTE = 60 * 1000;

    /**
     * 一小时的毫秒值，用于判断上次的更新时间
     */
    public static final long ONE_HOUR = 60 * ONE_MINUTE;

    /**
     * 一天的毫秒值，用于判断上次的更新时间
     */
    public static final long ONE_DAY = 24 * ONE_HOUR;

    /**
     * 一月的毫秒值，用于判断上次的更新时间
     */
    public static final long ONE_MONTH = 30 * ONE_DAY;

    /**
     * 一年的毫秒值，用于判断上次的更新时间
     */
    public static final long ONE_YEAR = 12 * ONE_MONTH;

    public static String dateEditorYMD(Date currentTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(currentTime);
        return time;
    }

    public static String dateEditorYMDHMS(Date currentTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(currentTime);
        return time;
    }

    /**
     * 计算过去发表时间是多久前
     *
     * @param lastUpdateTime
     * @return
     */
    public static String dateEditorTimeAgo(Long lastUpdateTime) {
        String date = transferLongToDate("yyyy-MM-dd HH:mm:ss",lastUpdateTime);
        long currentTime = System.currentTimeMillis();
        long timePassed = currentTime - lastUpdateTime;
        long timeIntoFormat;
        String updateAtValue;
        if (timePassed < ONE_MINUTE) { // 小于一分钟
            updateAtValue = "刚刚";
        } else if (timePassed < ONE_HOUR) {
            timeIntoFormat = timePassed / ONE_MINUTE;
            updateAtValue = timeIntoFormat + "分钟前";
        } else if (timePassed < ONE_DAY) {
            timeIntoFormat = timePassed / ONE_HOUR;
            updateAtValue = timeIntoFormat + "小时前";
        } else if (timePassed < ONE_MONTH) {
//            timeIntoFormat = timePassed / ONE_DAY;
//            updateAtValue = timeIntoFormat + "天前";
            updateAtValue = date.substring(date.indexOf("-")+1);
        } else if (timePassed < ONE_YEAR) {
//            timeIntoFormat = timePassed / ONE_MONTH;
//            updateAtValue = timeIntoFormat + "个月前";
            updateAtValue = date.substring(date.indexOf("-")+1);
        } else {
//            timeIntoFormat = timePassed / ONE_YEAR;
//            updateAtValue = timeIntoFormat + "年前";
            updateAtValue = date;
        }
        return updateAtValue;
    }

    /**
     * 把毫秒转化成日期
     *
     * @param dateFormat(日期格式，例如：MM/ dd/yyyy HH:mm:ss)
     * @param millSec(毫秒数)
     * @return
     */
    public static String transferLongToDate(String dateFormat, Long millSec) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        Date date = new Date(millSec);
        return sdf.format(date);
    }
}
